package org.wevil.vo;


import lombok.Data;
import org.springframework.http.HttpStatus;
import org.wevil.enumu.ResultEnum;

/**
 * 返回数据
 *
 * @author wevil
 * @since 2020/2/1
 */
@Data
public class Result<T> {

    /**
     * 状态码。见{@link org.wevil.enumu.ResultEnum}
     * */
    Integer code;
    /** 消息 */
    String msg;
    /** 返回内容 */
    T data;

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        this.data = null;
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Result<T> success(){
        return info(ResultEnum.SUCCESS);
    }

    public static <T> Result<T> success(T data){
        return info(ResultEnum.SUCCESS, data);
    }

    public static <T> Result<T> info(ResultEnum e) {
        return new Result<>(e.getCode(), e.getValue());
    }

    public static <T> Result<T> info(HttpStatus e) {
        return new Result<>(e.value(), e.getReasonPhrase());
    }

    public static <T> Result<T> info(ResultEnum e, T data) {
        return new Result<>(e.getCode(), e.getValue(), data);
    }

    public static <T> Result<T> info(ResultEnum e, String msg, T data) {
        return new Result<>(e.getCode(), msg, data);
    }
}
